package org.his.controller.modular.hushi;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhenzi.sms.ZhenziSmsClient;
import org.his.dao.CaseInfoDao;
import org.his.dao.ReserveDao;
import org.his.dao.UserDao;
import org.his.pojo.binglidangan.CaseInfo;
import org.his.pojo.liucheng.Reserve;
import org.his.pojo.quanxuan.User;
import org.his.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/yuyuedengji")
public class YuYueController {

    @Autowired
    private ReserveDao reserveDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CaseInfoDao caseInfoDao;

    @RequestMapping("/index")
    public String index()throws Exception{
        return "modular/hushi/yuyuedengji";
    }


    /*查询预约数据*/
    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String ,Object> getList(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10") Integer limit, Reserve reserve) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        Page<Reserve> pa = PageHelper.startPage(page,limit);
        List<Reserve> reserveList = reserveDao.getList(reserve);

        map.put("code",0);
        map.put("msg",null);
        map.put("count",pa.getTotal());
        map.put("data",reserveList);

        return map;
    }


    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String ,Object> batchDelete (Integer[] ids) throws Exception{
        Map<String ,Object> map = new HashMap<>();
        reserveDao.batchDelete(ids);
        return map;
    }

    @RequestMapping("/yuyuefrom")
    public String yuyueForm()throws Exception{

        return "modular/hushi/addyuyue";
    }

    @RequestMapping("/editfrom")
    public String editForm()throws Exception{
        return "modular/hushi/updyuyue";
    }

    @RequestMapping("/yanzheng")
    @ResponseBody
    public int yanzheng(String reserveMobile)throws Exception{
        return reserveDao.yanzheng(reserveMobile);
    }

    @RequestMapping("/yishengList")
    @ResponseBody
    public List<User> yishengList()throws  Exception{
        return reserveDao.yishengList();
    }


    /**
     * 预约添加
     * @throws Exception
     */
    @PostMapping("/addyuyue")
    @ResponseBody
    public Map<String,Object> addyuyue(Reserve reserve, HttpSession session) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        User login_user = (User) session.getAttribute(Common.LOGIN_USER);

        User yuyue = userDao.get(reserve.getUserId().intValue());

        String content = "尊敬的"+reserve.getReserveName()+",您预约"+yuyue.getUsername()+"医生成功！请在"+reserve.getReserveopentime()+"之前来医院就诊,您的病历号为："+reserve.getMedicalRecordNo()+"。翱翔his管理系统友情提示！";
        String apiUrl = "https://sms_developer.zhenzikj.com";
        String appId = "100458";
        String appSecret = "MDZkZTk4M2MtODJkYi00YTM5LWFkMDgtZTVjYzk0MjVkMDk3";

        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl,appId,appSecret);
        String result = client.send(reserve.getReserveMobile(), content,"dfee_dfdw_xdfd_dfdfd");

        reserve.setIsDiagnosis(0);
        reserve.setOperUserId(login_user);
        reserveDao.add(reserve);
        return map;
    }

    /**
     * 预约添加
     * @throws Exception
     */
    @PostMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(Reserve reserve, HttpSession session) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        User login_user = (User) session.getAttribute(Common.LOGIN_USER);

        reserve.setIsDiagnosis(0);
        reserve.setOperUserId(login_user);
        reserveDao.update(reserve);
        return map;
    }


    @RequestMapping("/retuenNull")
    @ResponseBody
    public Map<String ,Object> retuenNull (Integer userid) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        List<Reserve> reserveList = reserveDao.getReserveList(userid);

        map.put("code",0);
        map.put("msg",null);
        map.put("count",0);
        map.put("data",reserveList);
        return map;
    }

    @RequestMapping("/returnlishi")
    @ResponseBody
    public Map<String ,Object> returnlishi (String shenfen) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        List<CaseInfo> caseInfoList = caseInfoDao.getlishi(shenfen);

        map.put("code",0);
        map.put("msg",null);
        map.put("count",0);
        map.put("data",caseInfoList);
        return map;
    }

    @RequestMapping("/maxnum")
    @ResponseBody
    public Integer MaxNum()throws Exception{
        return reserveDao.MaxNum();
    }


    @RequestMapping("/get")
    @ResponseBody
    public Reserve get(Integer reserveId) throws Exception{
        return reserveDao.get(reserveId);
    }

    @RequestMapping("/huiyuan")
    @ResponseBody
    public int huiyuan(Integer membershipCardNo) throws Exception{
        return reserveDao.huiyuan(membershipCardNo);
    }


}
